package org.alis.rpc.common.paap.util;

import org.alis.rpc.common.paap.v2.PropertiesPlaceholderConfigurer;
import org.alis.rpc.common.util.AppContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.ConfigurablePropertyResolver;

import java.util.function.Function;

/**
 * @author lc
 * @version v2.6.3
 * @date 2020/9/4 15:51
 * @className AppPropertiesUtils.class
 */
public final class AppPropertiesUtils {



    private AppPropertiesUtils(){
        throw new IllegalStateException("非容器文件 不准加载到容器");
    }

    private static ConfigurablePropertyResolver getInstance(){
        return SingleConfigurer.propertiesPlaceholderConfigurer.getProp();
    }

    private static class SingleConfigurer{
        private static PropertiesPlaceholderConfigurer propertiesPlaceholderConfigurer = AppContextUtil.getBeanByClassType(PropertiesPlaceholderConfigurer.class);
    }


    public static String getValue(String key){
        return getValue(key, StringUtils.EMPTY);
    }

    public static String getValue(String key,String defaultValue){
        return getValue(key,String.class,defaultValue);
    }

    public static <T> T getValue(String key,Class<T> clazz) {
        Function<String,T> function = k->null;
        return getValue(key, clazz,function);

    }


    public static <T> T getValue(String key,Class<T> clazz,T defaultValue){
        return getInstance().getProperty(key,clazz,defaultValue);
    }

    public static <T> T getValue(String key, Class<T> clazz, Function<String,T> function){
        return getValue(key, clazz, function.apply(key));
    }


}
